package com.example.smallmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.smallmall.dto.SpGoodsQueryParam;
import com.example.smallmall.entity.SpGoods;
import com.example.smallmall.mapper.SpGoodsMapper;
import com.example.smallmall.service.ISpGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-20
 */
@Service
public class SpGoodsServiceImpl extends ServiceImpl<SpGoodsMapper, SpGoods> implements ISpGoodsService {
    @Resource
    private SpGoodsMapper spGoodsMapper;

    @Override
    public Map<String, Object> selectPage(SpGoodsQueryParam queryParam, int pageNum, int pageSize) {
        Page<SpGoods> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SpGoods> queryWrapper = new QueryWrapper<>();
        if (queryParam != null) {
            if (queryParam.getGoodsName() != null) {
                queryWrapper.like("goods_name", queryParam.getGoodsName());
            } else if (queryParam.getGoodsNumber() != null) {
                queryWrapper.le("goods_number", queryParam.getGoodsNumber());
            } else if (queryParam.getGoodsPrice() != null) {
                queryWrapper.le("goods_price", queryParam.getGoodsPrice());
            } else if (queryParam.getCatOneId() != null) {
                queryWrapper.eq("cat_one_id", queryParam.getCatOneId());
            } else if (queryParam.getCatTwoId() != null) {
                queryWrapper.eq("cat_two_id", queryParam.getCatTwoId());
            } else if (queryParam.getCatThreeId() != null) {
                queryWrapper.eq("cat_three_id", queryParam.getCatThreeId());
            } else if (queryParam.getIsPromote() != null) {
                queryWrapper.eq("is_promote", queryParam.getIsPromote());
            }
        }
        IPage<SpGoods> iPage = spGoodsMapper.selectPage(page, queryWrapper.orderByDesc("goods_id"));
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("total", iPage.getTotal());
        stringObjectMap.put("pagenum", iPage.getCurrent());
        stringObjectMap.put("goods", iPage.getRecords());
        return stringObjectMap;
    }

    @Override
    public SpGoods selectById(int id) {
        return spGoodsMapper.selectById(id);
    }
}
